package JZ;

public class jz37 {
    static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    static String  re = "";
    static int index = -1;
    public static void main(String[] args) {
        TreeNode node = new TreeNode(100);
        node.left = new TreeNode(50);
        node.left.right = new TreeNode(150);
        String s = Serialize(node);
        TreeNode t = Deserialize(s);
    }
    static String Serialize(TreeNode root) {
        if (root == null)
            return "#!";
        dfs(root);
        return re;
    }
    static void dfs(TreeNode root){
        if(root != null){
            re += root.val + "!";
            dfs(root.left);
            dfs(root.right);
            return;
        }
        re += "#!";
    }
    static TreeNode Deserialize(String str) {
        if(str == null || str.equals("#!") || str.equals(""))
            return null;
        String[] a = str.split("!");
        return dfs2(a);
    }
    static TreeNode dfs2(String[] str){
        index++;
        if(index == str.length)
            return null;
        if(str[index].equals("#"))
            return null;
        TreeNode root = new TreeNode(Integer.parseInt(str[index]));
        root.left = dfs2(str);
        root.right = dfs2(str);
        return root;
    }
}
